<?php

namespace api\modules\app\services;

use api\common\helpers\Constant;
use api\modules\app\models\NotificationRecord;
use Yii;

class CacheService
{
    public $cache;

    const HISTORY_SEARCH_FIELD_PREX = 'history_search_field:';

    public function __construct()
    {
        $this->cache = Yii::$app->redisCache;
    }

    public function getSearchField($userId)
    {
        $key = self::HISTORY_SEARCH_FIELD_PREX . $userId;
        $data = $this->cache->get($key);

        return !empty($data) ? json_decode($data) : [];
    }

    public function setSearchField($userId, $data)
    {
        $key = self::HISTORY_SEARCH_FIELD_PREX . $userId;
        return $this->cache->set($key, json_encode($data));
    }

    public function clearSearchField($userId)
    {
        $key = self::HISTORY_SEARCH_FIELD_PREX . $userId;
        return $this->cache->delete($key);
    }

    public function removeUserTokenCache($token)
    {
        $this->clearUserInfoByToken($token);
        $this->clearEventCountByToken($token);
        $this->clearUserActionByToken($token);
    }

    public function cacheUserInfoByToken($token, $info)
    {
        $key = Constant::LOGIN_USER_CACHE . '_' . $token;
        $res = $this->cache->set($key, json_encode($info), -1);
        if (empty($res)) {
            throw new \Exception('用户身份缓存失败');
        }
    }

    public function clearUserInfoByToken($token)
    {
        $key = Constant::LOGIN_USER_CACHE . '_' . $token;
        $this->cache->delete($key);
    }

    public function cacheUserActionByToken($token, $data)
    {
        $key = Constant::USER_ACTION_CACHE_KEY . '_' . $token;
        $res = $this->cache->set($key, $data, -1);
        return $res;
    }

    public function getUserActionByToken($token)
    {
        $key = Constant::USER_ACTION_CACHE_KEY . '_' . $token;
        $data = $this->cache->get($key);
        if (!$data) {
            return false;
        }
        return $data;
    }

    public function clearUserActionByToken($token)
    {
        $key = Constant::USER_ACTION_CACHE_KEY . '_' . $token;
        $this->cache->delete($key);
    }

    public function clearEventCountByToken($token)
    {
        $key = Constant::MENU_COUNT_CACHE_KEY . $token;
        $this->cache->delete($key);
    }

    public function getEventCountByToken($token)
    {
        $key = Constant::MENU_COUNT_CACHE_KEY . $token;

        return $this->cache->get($key);
    }

    public function setEventCountByToken($token, $value, $ttl)
    {
        $key = Constant::MENU_COUNT_CACHE_KEY . $token;

        return $this->cache->set($key, $value, $ttl);
    }

    public function getUserInfoByToken($token)
    {
        $key = Constant::LOGIN_USER_CACHE . '_' . $token;
        return json_decode($this->cache->get($key), true);
    }

    public function getUnreadMessageCount($user)
    {
        $key = 'paidan30_unread_count_' . $user['id'];
        $count = $this->cache->get($key);
        if (!empty($count)) {
            return $count > 0 ? $count : 0;
        }
        $condition = [
            'user_id' => $user['id'],
            'state' => 0,
        ];
        $count = NotificationRecord::find()->where($condition)->count();
        $this->cache->set($key, empty($count) ? -1 : $count, 5 * 60);

        return $count;
    }
}